#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Purpose: Session related methods
# Created: 04/04/2009
# $Id $
# Copyright (c) 2008,2009 by University College London
# Authors:
# Didrik Pinte --<dpinte@dipole-consulting.com>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with HydroPlatform for details.
'''
Facility functions related to the use of the Thuban session
'''

from hydroplatform.model.layer import NetworkLayer

def has_dbsession(context):
    '''
    Does the Thuban context have a dbsession object in the session ?
    '''
    if hasattr(context.session, "dbsession") \
        and context.session.dbsession is not None:
        return True
    else: 
        return False
    
def has_project(context):
    '''
    Does the Thuban context have a project object in the session ?
    '''
    if hasattr(context.session, 'hpproject') \
       and context.session.hpproject is not None:
        return True
    else: 
        return False

def has_network(context):
    '''
    Does the Thuban context have a network object in the session ?
    '''
    if hasattr(context.session, 'hpnetwork') \
       and context.session.hpnetwork is not None:
        return True
    else: 
        return False

    
    
def remove_loaded_networks(context):
    """
    Remove the currently loaded networks from memory and ui
    """
    map = context.mainwindow.canvas.Map()
    # close the previous network if any
    for layer in map.Layers():
        if isinstance(layer, NetworkLayer):
                map.RemoveLayer(layer)        
                layer = None 